package com.xue566.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xue566.dao.SchoolMapper;
import com.xue566.entity.SchoolEntity;
import com.xue566.enums.StateEnum;
import com.xue566.service.ISchoolService;
import com.xue566.util.ResultBody;
import com.xue566.util.StateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 21:59 2020/8/16
 * @return
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, SchoolEntity> implements ISchoolService {

    /**
     * @Author: Wuzh
     * @Description: 分页条件模糊查询记录
     * @Data:
     */
    @Override
    public ResultBody<SchoolEntity> selectAllSchool (SchoolEntity schoolEntity) {
        Page<SchoolEntity> pages = new Page<>(schoolEntity.getPageNum(), schoolEntity.getPageSize());
        QueryWrapper<SchoolEntity> queryWrapper = new QueryWrapper<>();
        if ("".equals(schoolEntity.getTrackId()) || null == schoolEntity.getTrackId()) {
            throw new StateException(StateEnum.TRACK_NULL_EXCE);
        }
        // 组装条件查询时带来的条件
        if (StringUtils.isNotBlank(schoolEntity.getCenterId())) {
            queryWrapper.eq("centerId", schoolEntity.getCenterId());
        }
        if (StringUtils.isNotBlank(schoolEntity.getSchoolName())) {
            queryWrapper.eq("schoolName", schoolEntity.getSchoolName());
        }
        if (StringUtils.isNotBlank(schoolEntity.getSchoolAddress())) {
            queryWrapper.like("schoolAddress", schoolEntity.getSchoolAddress()); // 调用默认like方法
        }
        if (0 != schoolEntity.getType()) {
            queryWrapper.eq("type", schoolEntity.getType());
        }
        if (0 != schoolEntity.getLife()) {
            queryWrapper.eq("life", schoolEntity.getLife());
        }
        if (StringUtils.isNotBlank(schoolEntity.getUserId())) {
            queryWrapper.eq("userId", schoolEntity.getUserId());
        }
        if (StringUtils.isNotBlank(schoolEntity.getCreateTime())) {
            queryWrapper.eq("createTime", schoolEntity.getCreateTime());
        }
        if (0 != schoolEntity.getIsUsed()) {
            queryWrapper.eq("isUsed", schoolEntity.getIsUsed());
        }
        IPage<SchoolEntity> pageResults = baseMapper.selectPage(pages, queryWrapper);
        return ResultBody.success(schoolEntity.getPageNum(), schoolEntity.getPageSize(), pageResults.getTotal(), pageResults.getRecords(), "/school");
    }


    /**
     * @Author: Wuzh
     * @Description: 保存学校信息
     * @Data:
     */
    public ResultBody<SchoolEntity> saveSchool (SchoolEntity schoolEntity) {
        if (schoolEntity == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        if ("".equals(schoolEntity.getSchoolId()) || schoolEntity.getSchoolId() == null) {
            throw new StateException(StateEnum.PRIMARY_KEY_EXCE);
        }
        if ("".equals(schoolEntity.getTrackId()) || null == schoolEntity.getTrackId()) {
            throw new StateException(StateEnum.TRACK_NULL_EXCE);
        }
        // 保存数据库, 当上面没拦截成，执行插入时导致异常会被拦截成了ERROR级别错误
        int rInt = baseMapper.insert(schoolEntity);
        if (rInt == 0) {
            return ResultBody.fail(StateEnum.INSERT_EXCE.getCode(), StateEnum.INSERT_EXCE.getMessage());
        }
        return ResultBody.success("school");
    }

    /**
     * @Author: Wuzh
     * @Description: 查询一条记录
     * @Data:
     */
    public ResultBody<SchoolEntity> selectSchool (SchoolEntity schoolEntity) {
        if (schoolEntity == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        if ("".equals(schoolEntity.getSchoolId()) || schoolEntity.getSchoolId() == null) {
            throw new StateException(StateEnum.PRIMARY_KEY_EXCE);
        }
        if ("".equals(schoolEntity.getTrackId()) || null == schoolEntity.getTrackId()) {
            throw new StateException(StateEnum.TRACK_NULL_EXCE);
        }
        schoolEntity = baseMapper.selectById(schoolEntity.getSchoolId());
        if (schoolEntity == null) {
            return ResultBody.fail(StateEnum.SELECT_NULL_EXCE.getCode(), StateEnum.SELECT_NULL_EXCE.getMessage());
        }
        return ResultBody.success(schoolEntity, "school");
    }


    /**
     * @Author: Wuzh
     * @Description: 修改学校记录
     * @Data:
     */
    public ResultBody<SchoolEntity> updateSchool (SchoolEntity schoolEntity) {
        if (schoolEntity == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        if ("".equals(schoolEntity.getSchoolId()) || schoolEntity.getSchoolId() == null) {
            throw new StateException(StateEnum.PRIMARY_KEY_EXCE);
        }
        if ("".equals(schoolEntity.getTrackId()) || null == schoolEntity.getTrackId()) {
            throw new StateException(StateEnum.TRACK_NULL_EXCE);
        }
        UpdateWrapper<SchoolEntity> updateWrapper = new UpdateWrapper<SchoolEntity>().eq("schoolId", schoolEntity.getSchoolId());
        int uInt = baseMapper.update(schoolEntity, updateWrapper);
        if (uInt == 0) {
            return ResultBody.fail(StateEnum.UPDATE_EXCE.getCode(), StateEnum.UPDATE_EXCE.getMessage());
        }
        return ResultBody.success("school");
    }


    /**
     * @Author: Wuzh
     * @Description: 删除学校记录
     * @Data:
     */
    public ResultBody<SchoolEntity> deleteSchool (SchoolEntity schoolEntity) {
        if (schoolEntity == null) {
            throw new StateException(StateEnum.REQUEST_PARAM_EXCE);
        }
        if ("".equals(schoolEntity.getSchoolId()) || schoolEntity.getSchoolId() == null) {
            throw new StateException(StateEnum.PRIMARY_KEY_EXCE);
        }
        if ("".equals(schoolEntity.getTrackId()) || null == schoolEntity.getTrackId()) {
            throw new StateException(StateEnum.TRACK_NULL_EXCE);
        }
        int rInt = baseMapper.deleteById(schoolEntity.getSchoolId());
        if (rInt == 0) {
            return ResultBody.fail(StateEnum.DELETE_EXCE.getCode(), StateEnum.DELETE_EXCE.getMessage());
        }
        return ResultBody.success("school");
    }

}
